package com.emapp.lccode.activity

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.os.Bundle
import android.widget.DatePicker
import androidx.fragment.app.Fragment
import com.emapp.lccode.R
import com.emapp.lccode.adapter.ViewPgerKtAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.databinding.ActivityDeviceBinding
import com.emapp.lccode.fragment.DeviceFragment
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.toStartActivity
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gyf.immersionbar.ktx.immersionBar
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.Calendar

/**
 */
class DeviceKtActivity : BaseActivity1<BaseViewModel, ActivityDeviceBinding>(),
    OnDateSetListener {
    var titles: ArrayList<String> = arrayListOf("全部", "待审批", "审批通过", "审批拒绝", "已完成")
    var fragments: ArrayList<Fragment> = arrayListOf()
    private lateinit var dialog: DatePickerDialog
    private val adapter: ViewPgerKtAdapter by lazy { ViewPgerKtAdapter(this, fragments) }
    private var time_flag=1
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
        mViewBind.lvTitle.tvTitle.text = "设施设备"
        mViewBind.lvTitle.tvRight.text = "添加"
        EventBus.getDefault().register(this)
        fragments.add(DeviceFragment.newInstance("0"))
        fragments.add(DeviceFragment.newInstance("1"))
        fragments.add(DeviceFragment.newInstance("2"))
        fragments.add(DeviceFragment.newInstance("3"))
        fragments.add(DeviceFragment.newInstance("4"))
        mViewBind.viewpager.adapter = adapter
        //mViewBind.viewpager.offscreenPageLimit = 3
        mViewBind.layoutTab.tabMode = TabLayout.MODE_SCROLLABLE
        TabLayoutMediator(mViewBind.layoutTab, mViewBind.viewpager) { tab, position ->
            tab.text = titles[position]
        }.attach()
        //initCalendar()
    }

    override fun createObserver() {
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.lvTitle.tvRight
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }
                R.id.tv_right -> {
                    toStartActivity(DeviceAddKtActivity::class.java)
                }

//                R.id.tv_start -> {
//                    time_flag = 1
//                    dialog.show()
//                }
//
//                R.id.tv_end -> {
//                    time_flag = 2
//                    dialog.show()
//                }
//                R.id.tv_search -> {
//
//                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.FINISH -> {
            }

            else -> {}
        }
    }
    private fun initCalendar() {
        val calendar = Calendar.getInstance()
        dialog = DatePickerDialog(
            this, this, calendar[Calendar.YEAR] //年份
            , calendar[Calendar.MONTH] //月份
            , calendar[Calendar.DAY_OF_MONTH]
        ) //日子
    }
    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        //val desc = String.format("%s-%02d-%02d", year, month + 1, dayOfMonth)
        val desc = String.format("%s-%d-%d", year, month + 1, dayOfMonth)
//        if (time_flag == 1) {
//            mViewBind.tvStart.text=desc
//            EventBus.getDefault().postSticky(EventBusModel(EventBusConfig.SET_START_DATE,desc))
//        } else {
//            mViewBind.tvEnd.text=desc
//            EventBus.getDefault().postSticky(EventBusModel(EventBusConfig.SET_END_DATE,desc))
//        }
    }

}
